package org.csu.mypetstoreplus.service;

import org.csu.mypetstoreplus.entity.Inventory;
import org.csu.mypetstoreplus.persistence.*;
import org.csu.mypetstoreplus.vo.ItemVO;

import org.csu.mypetstoreplus.entity.Category;
import org.csu.mypetstoreplus.entity.Product;
import org.csu.mypetstoreplus.entity.Item;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private ItemDao itemDao;

    //search
    public List<Category> getCategories(){
        return categoryDao.findAll();
    }

    public List<Product> getProducts(){
        return productDao.findAll();
    }

    public List<Product> getProductsByCategoryId(String categoryId){
        return productDao.findByCategoryId(categoryId);
    }

    public List<ItemVO> getItemVOsByProductId(String productId){
        List<Item> items = itemDao.findByProductId(productId);
        List<ItemVO> itemVOs = new ArrayList<>();
        for (Item item : items){
            Inventory inventory = inventoryDao.findByItemId(item.getItemId());
            itemVOs.add(new ItemVO(item, inventory));
        }
        return itemVOs;
    }

    public Category getCategoryById(int id){
        return categoryDao.findById(id).get();
    }

    public Product getProductById(int id){
        return productDao.findById(id).get();
    }
}

